<?php
namespace app\agentapi\controller;
use app\agentapi\model\User as AgentUser;
use think\Cache;
/**
 * 登录控制器
 */
class Passport extends Controller{
    /**
     * 账号密码登录
     */
    public function login(){
        //提交登录信息
        $model = new AgentUser;
        $userName=input('post.user_name');
        $password=input('post.password');
        $code=input('post.code');
        if(empty($userName)){
            return json(['msg'=>'用户名称不能为空','code'=>'error','data'=>[]]);
        }
        if(empty($password)){
            return json(['msg'=>'用户密码不能为空','code'=>'error','data'=>[]]);
        }
        $return=$model->agentLogin($userName,$password,$code);
        if($return['code']==1){
            $returns=$return['data'];
            $returns['token']=Cache::get($return['data']['id'].'token');
            //获取授权信息
            return json(['msg'=>'登陆成功','code'=>'success','data'=>$returns]);
        }elseif($return['code']==-1){
            return json(['msg'=>'登录失败，账号或密码错误','code'=>'error','data'=>[]]);
        }elseif($return['code']==-3){
            return json(['msg'=>'账号类型不正确','code'=>'error','data'=>[]]);
        }else{
            return json(['msg'=>'该账号已被删除','code'=>'error','data'=>[]]);
        }
    }
    /**
     * curl请求
     */
    private function get_request($url){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec($curl);
        curl_close($curl);
        return $data;
    }
}
